package com.examstack.portal.security.handler;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyLogoutHandler extends AbstractAuthenticationTargetUrlRequestHandler implements LogoutSuccessHandler {
    private String targetUrl;
    private String mobileDefaultTargetUrl;
    public MyLogoutHandler(){

    }

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        if(httpServletRequest.getHeader("referer").contains("mobile")) {
            setDefaultTargetUrl(mobileDefaultTargetUrl);
        }else{
            setDefaultTargetUrl(targetUrl);
        }
        super.handle(httpServletRequest, httpServletResponse, authentication);
    }

    public String getTargetUrl() {
        return targetUrl;
    }

    public void setTargetUrl(String targetUrl) {
        this.targetUrl = targetUrl;
    }

    public String getMobileDefaultTargetUrl() {
        return mobileDefaultTargetUrl;
    }

    public void setMobileDefaultTargetUrl(String mobileDefaultTargetUrl) {
        this.mobileDefaultTargetUrl = mobileDefaultTargetUrl;
    }
}
